package cn.com.pxto.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import cn.com.pxto.commons.httputil.ParamUtil;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.commons.pagesupport.PageUtils;
import cn.com.pxto.model.LessonType;
import cn.com.pxto.model.School;
import cn.com.pxto.service.SchoolManager;

public class SchoolAction extends DispatchAction {
	private Log log  = LogFactory.getLog(SchoolAction.class);
	private SchoolManager schoolManager=null;
	
	public ActionForward preCreateSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preCreateSchool method");
		List<LessonType> lessonTypeList=schoolManager.listAllLessonType();
		if(lessonTypeList == null && lessonTypeList.size() == 0){
			return new ActionForward("lessonType.do?method=preCreateLessonType");
		}else{
			request.setAttribute("lessonTypeList", lessonTypeList);
			return mapping.findForward("preCreateSchool");
		}
	}
	
	public ActionForward doCreateSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doCreateSchool method");
//		ltid,name
		DynaActionForm schoolForm = (DynaActionForm)form;
		School school = (School)schoolForm.get("school");
		boolean s=true;
		if(schoolManager.getLessonTypeByName(school.getName()) == null){
			String[] strArray=request.getParameterValues("lessonTypeStr");
			school.setLessonTypeStr(toString(strArray));
			schoolManager.createSchool(school);
		}else{
			request.setAttribute("message", "学校已存在!");
			s=false;
		}
		request.setAttribute("boolean",s);
		request.getSession().getServletContext().setAttribute("schoolHasNew", true);
		return mapping.findForward("success");
	}
	
	public ActionForward preUpdateSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preUpdateSchool method");
		int sid=ParamUtil.getIntParams(request, "id");
		List<LessonType> ltList=schoolManager.listAllLessonType();
		School school=schoolManager.getSchool(sid);
		request.setAttribute("school", school);
		request.setAttribute("ltList", ltList);
		return mapping.findForward("preUpdateSchool");
	}
	
	public ActionForward doUpdateSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doUpdateSchool method");
		DynaActionForm schoolForm = (DynaActionForm)form;
		School school = (School)schoolForm.get("school");
		String[] strArray=request.getParameterValues("lessonTypeStr");
		school.setLessonTypeStr(toString(strArray));
		schoolManager.updateSchool(school);
		request.getSession().getServletContext().setAttribute("schoolHasNew", true);
		return listSchool(mapping, form, request, response);
	}
	
	public ActionForward deleteSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in deleteSchool method");
		int sid=ParamUtil.getIntParams(request, "id");
		schoolManager.deleteSchool(sid);
		return listSchool(mapping, form, request, response);
	}
	
	public ActionForward listSchool(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in listSchool method");
		PageInfo pageInfo=PageUtils.getPageInfo(request);
		List<School> schoolList=schoolManager.listSchool(pageInfo);
		request.setAttribute("schoolList", schoolList);
		request.setAttribute("pageInfo",pageInfo);
		return mapping.findForward("listSchool");
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}
	
	 private static String toString(Object[] a) {
	        if (a == null)
	            return "null";
		int iMax = a.length - 1;
	        if (iMax == -1)
	            return "";

	        StringBuilder b = new StringBuilder();
	        //添加前缀
	        b.append(",");
	        for (int i = 0; ; i++) {
	            b.append(String.valueOf(a[i]));
	            if (i == iMax)
			return b.append(",").toString();
		    b.append(",");
	        }
	    }

}
